#include <gui/modules/menu.h>
#include <gui/modules/popup.h>

#include "starting_scene.h"
#include "../app_context.h"
#include "../tone_gen.h"
#include "../utils/linked_list.h"

// Icons to include
#include "tone_gen_icons.h"

/** indices for menu items */
typedef enum {
    ToneGenAppMenuSelection_Play,
    ToneGenAppMenuSelection_Adjust
} ToneGenAppMenuSelection;

/** main menu callback - sends a custom event to the scene manager based on the menu selection */
void menu_callback_starting_scene(void* context, uint32_t index) {
    FURI_LOG_I(TAG, "menu_callback_starting_scene");
    UNUSED(context);
    struct AppContext_t* app = context;
    switch(index) {
    case ToneGenAppMenuSelection_Play:
        scene_manager_handle_custom_event(app->scene_manager, ToneGenAppMenuSelection_Play);
        break;
    case ToneGenAppMenuSelection_Adjust:
        scene_manager_handle_custom_event(app->scene_manager, ToneGenAppMenuSelection_Adjust);
        break;
    }
}

/** resets the menu, gives it content, callbacks and selection enums */
void scene_on_enter_starting_scene(void* context) {
    FURI_LOG_I(TAG, "scene_on_enter_starting_scene");
    struct AppContext_t* app = (struct AppContext_t*)context;
    struct View_t* menuView = app->activeViews[ToneGenAppView_SharedMenu];

    // Set the currently active view
    menu_reset(menuView->viewData);

    // NB. icons are specified as NULL below, because:
    // * icons are _always_ animated by the menu
    // * the icons provided (&I_one, &I_two) are generated by the build process
    // * these icons do not have a framerate (resulting in a division by zero)
    menu_add_item(
        menuView->viewData,
        "Play Tone",
        &A_play_button,
        ToneGenAppMenuSelection_Play,
        menu_callback_starting_scene,
        app);
    menu_add_item(
        menuView->viewData,
        "Adjust Tone",
        &A_settings_button,
        ToneGenAppMenuSelection_Adjust,
        menu_callback_starting_scene,
        app);
    view_dispatcher_switch_to_view(app->view_dispatcher, ToneGenAppView_SharedMenu);
}

/** main menu event handler - switches scene based on the event */
bool scene_on_event_starting_scene(void* context, SceneManagerEvent event) {
    FURI_LOG_I(TAG, "scene_on_event_starting_scene");
    UNUSED(context);
    struct AppContext_t* app = context;
    bool consumed = false;
    switch(event.type) {
    case SceneManagerEventTypeCustom:
        switch(event.event) {
        case ToneGenAppMenuSelection_Play:
            scene_manager_next_scene(app->scene_manager, ToneGenAppScene_Playback);
            consumed = true;
            break;
        case ToneGenAppMenuSelection_Adjust:
            scene_manager_next_scene(app->scene_manager, ToneGenAppScene_Settings);
            consumed = true;
            break;
        }
        break;
    default: // eg. SceneManagerEventTypeBack, SceneManagerEventTypeTick
        consumed = false;
        break;
    }
    return consumed;
}

void scene_on_exit_starting_scene(void* context) {
    FURI_LOG_I(TAG, "scene_on_exit_starting_scene");
    UNUSED(context);
}
